package com.system_jsp.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system_jsp.bean.UserBean;
import com.system_jsp.bean.pageBean;
import com.system_jsp.dao.UserDao;

/**
 * Servlet implementation class UserList
 */
@WebServlet(description = "用户列表信息处理", urlPatterns = { "/UserList" })
public class UserList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		pageBean pb = new pageBean();
		int pageSize = 5;
		try {
			pageSize = Integer.parseInt(getServletContext().getInitParameter(
					"pageSize").toString());
		} catch (Exception e) {
			pageSize = 5;
		}
		pb.setPageSize(pageSize);
		
		int totalRecord = new UserDao().totalRecord();
		pb.setRecordTotal(totalRecord);
		int pageTotal = totalRecord % pageSize == 0 ? totalRecord / pageSize
				: totalRecord / pageSize + 1;
		pb.setPageTotal(pageTotal);

		String pageStr = request.getParameter("page");
		int pageNow = pageStr == null ? 1 : Integer.parseInt(pageStr);
		if (pageNow <= 0) {
			pageNow = 1;
		}
		if (pageNow > pageTotal) {
			pageNow = pageTotal;
		}
		pb.setPageNow(pageNow);

		List<UserBean> list = new UserDao().search(pageSize, pageNow);
		//System.out.println(list);

		request.setAttribute("list", list);
		request.setAttribute("pageTotal", pageTotal);
		request.setAttribute("pageNow", pageNow);

		request.getRequestDispatcher("list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		// TODO Auto-generated method stub
	}

}
